﻿using BW.IBLL;
using BW.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class shipuController : ControllerBase
    {
        public shipuController(IshipuBLL bll,IWebHostEnvironment web,ILogger<shipuController> logger)
        {
            Bll = bll;
            Web = web;
            Logger = logger;
        }

        public IshipuBLL Bll { get; }
        public IWebHostEnvironment Web { get; }
        public ILogger<shipuController> Logger { get; }
        /// <summary>
        /// 食谱添加
        /// </summary>
        /// <param name="m">参数</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> tinajia(shipuModel m)
        {
            try
            {
                Logger.LogInformation("食谱添加成功");
                return Bll.tinajia(m);
            }
            catch (Exception)
            {
                Logger.LogError("食谱添加失败");
                throw;
            }
        }
        /// <summary>
        /// 食谱显示
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="Fid">分类id</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public dynamic xianshi(string? name, int Fid)
        {
            try
            {
                Logger.LogInformation("食谱显示成功");
                return Bll.xianshi(name, Fid);
            }
            catch (Exception)
            {
                Logger.LogError("食谱显示失败");
                throw;
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">类型</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public IActionResult shangchuan(IFormFile file)
        {
            try
            {
                Logger.LogInformation("图片上传成功");
                string a = Guid.NewGuid().ToString();
                string b = Web.WebRootPath + @"\img\" + a + file.FileName;

                using (FileStream c = System.IO.File.Create(b))
                {
                    file.CopyTo(c);
                    c.Close();
                }
                return Ok("/img/" + a + file.FileName);
            }
            catch (Exception)
            {
                Logger.LogError("图片上传失败");
                throw;
            }
        }
    }
}
